package com.dhecp.project.storage.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.business.domain.ProductActive;
import com.dhecp.project.business.mapper.ProductActiveMapper;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.storage.domain.AssemblyMaintain;
import com.dhecp.project.storage.mapper.AssemblyMaintainMapper;
import com.dhecp.project.storage.service.AssemblyMaintainService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: kjecp
 * @description: 仓储-商品组装方案维护
 * @author: hlf
 * @create: 2022-09-05 09:14
 */
@Service
public class AssemblyMaintainServiceImpl extends BaseModuleServiceImpl implements AssemblyMaintainService {


    @Autowired
    private AssemblyMaintainMapper storageMapper;

    @Autowired
    private UpdateLogService updateLogService;

    @Autowired
    private ProductActiveMapper productActiveMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.AssemblyMaintain
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public AssemblyMaintain findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return storageMapper.findById(params);
    }


    /**
     * 
     */
    @Override
    public AssemblyMaintain findByProductID(String productID) throws Exception {
        return storageMapper.findByProductID(productID);
    }


    /**
     * @Description: 查询
     * @Param: [params]
     * @return: java.util.List<com.dhecp.project.storage.domain.AssemblyMaintain>
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public List<AssemblyMaintain> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));    //常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        PageUtils.startPage();
        return storageMapper.select(params);
    }

    /**
     * @Description: 保存
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult saveData(Map<String, Object> params) {

        AssemblyMaintain oldData = new AssemblyMaintain();

        AssemblyMaintain assemblyMaintain = JSON.parseObject(params.get("infoItems").toString(), AssemblyMaintain.class);

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("guid",assemblyMaintain.getGuid());
        oldData = storageMapper.findById(params2);


        if (assemblyMaintain.getGuid() != null && !assemblyMaintain.getGuid().equals("")){
            assemblyMaintain.setUpdateTime(new Date());
            assemblyMaintain.setUpdateUser(ShiroUtils.getUserId());
            storageMapper.update(assemblyMaintain);
            updateLogService.addUpdateLog(params.get("moduleTable").toString(),assemblyMaintain.getGuid(),oldData,assemblyMaintain,"修改");
        }else{
            assemblyMaintain.setGuid(RandomValueUtils.getGUID());
            assemblyMaintain.setCreateTime(new Date());
            assemblyMaintain.setCreateUser(ShiroUtils.getUserId());
            storageMapper.insert(assemblyMaintain);
            updateLogService.addInsertLog(params.get("moduleTable").toString(),assemblyMaintain.getGuid(),"新增");
        }
        if(assemblyMaintain.getProductID() !=null){
            Map<String, Object> params3 = new HashMap<String, Object>();
            params3.put("code",assemblyMaintain.getProductID());
            ProductActive productActive = productActiveMapper.findById(params3);
            productActive.setSizeLength(assemblyMaintain.getProductLength());
            productActive.setSizeWeight(assemblyMaintain.getProductWeight());
            productActive.setSizeWidth(assemblyMaintain.getProductWidth());
            productActive.setSizeHeight(assemblyMaintain.getProductHeight());
            productActiveMapper.updateProductActive(productActive);
        }

        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), assemblyMaintain.getGuid());
    }

    /**
     * @Description: 删除
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<AssemblyMaintain> businessDataList = JSON.parseArray(params.get("infoItems").toString(), AssemblyMaintain.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (AssemblyMaintain businessData : businessDataList) {
            storageMapper.delete(businessData);
            updateLogService.addInsertLog(params.get("moduleTable").toString(),businessData.getGuid(),"删除");
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }


}